﻿using CommunityToolkit.Mvvm.DependencyInjection;
using Syncfusion.UI.Xaml.Grid;
using Syncfusion.UI.Xaml.Grid.Helpers;
using TS2MP4.ViewModels;

namespace TS2MP4.Views;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainView
{
    //To get the calculated height from GetAutoRowHeight method.
    private double autoHeight = double.NaN;

    private readonly GridRowSizingOptions gridRowResizingOptions = new();

    public MainView()
    {
        InitializeComponent();
        DataContext = Ioc.Default.GetRequiredService<MainViewModel>();
    }

    private void SfDataGrid_QueryRowHeight(object sender, QueryRowHeightEventArgs e)
    {
        if (!SfDataGrid.GridColumnSizer.GetAutoRowHeight(e.RowIndex, gridRowResizingOptions, out autoHeight)) return;
        if (!(autoHeight > 30)) return;
        e.Height = autoHeight;
        e.Handled = true;
    }

    private void Window_SizeChanged(object sender, System.Windows.SizeChangedEventArgs e)
    {
        SfDataGrid.GetVisualContainer().RowHeightManager.Reset();
        SfDataGrid.GetVisualContainer().InvalidateMeasureInfo();
    }
}